package com.cheng.example.java.basics.Serialize;


import java.io.*;

public class SerializeDemo {
    public static void main(String[] args) {
        Hello h = new Hello();
        h.setName("程帅");
        //准备一个文件用于存储该对象的信息
        File f = new File("hello.obj");
        try (FileOutputStream fos = new FileOutputStream(f);
             ObjectOutputStream oos = new ObjectOutputStream(fos);
             FileInputStream fis = new FileInputStream(f);
             ObjectInputStream ois = new ObjectInputStream(fis)
        ) {
            //序列化对象，写入到磁盘中
            oos.writeObject(h);
            //反序列化对象
            Hello newHello = (Hello) ois.readObject();
            //测试方法
            newHello.sayWorld();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}